using Godot;
using System;
using System.ComponentModel;
using System.Dynamic;
using System.Runtime;

public partial class Enemy : Node2D
{
	public const float Speed = 300.0f;
	public const float JumpVelocity = -400.0f;

	public int Hp = 100;

	public bool isSpecial = false;
	public bool isAttack = false;

	public bool isPursue = false;
	public bool isBeAttack1 = false;
	public bool isBeAttack2 = false;

	private AnimationTree _animeTree;

	private AnimationNodeStateMachinePlayback _playBack;

	// Get the gravity from the project settings to be synced with RigidBody nodes.
	//public float gravity = ProjectSettings.GetSetting("physics/2d/default_gravity").AsSingle();

	public Hero _heroBody = null;

	private AnimationPlayer _playerNode = null;
	public override void _Ready()
	{
		//BodyEntered += onCollision;
		//BodyExited += onExit;
		_animeTree = GetNode<AnimationTree>("AnimationTree");
		_playBack = (AnimationNodeStateMachinePlayback)_animeTree.Get("parameters/StateMachine/playback");


	}

	/*private void onExit(Node2D body)
	{
		GD.Print(body.Name);
	}*/


	/*private void onCollision(Node2D body)
	{
		GD.Print(body.Name);
	}*/

	public override void _PhysicsProcess(double delta)
	{
		/*Vector2 velocity = Velocity;

		// Add the gravity.
		if (!IsOnFloor())
			velocity.Y += gravity * (float)delta;

		// Handle Jump.
		if (Input.IsActionJustPressed("ui_accept") && IsOnFloor())
			velocity.Y = JumpVelocity;

		// Get the input direction and handle the movement/deceleration.
		// As good practice, you should replace UI actions with custom gameplay actions.
		Vector2 direction = Input.GetVector("ui_left", "ui_right", "ui_up", "ui_down");
		if (direction != Vector2.Zero)
		{
			velocity.X = direction.X * Speed;
		}
		else
		{
			velocity.X = Mathf.MoveToward(Velocity.X, 0, Speed);
		}

		Velocity = velocity;
		MoveAndSlide();*/

	}

	private void OnCollisionBody(Rid bodyRid, Node2D body, long bodyShapeIndex, long localShapeIndex)
	{
		GD.Print("碰撞啦", bodyRid, " ", body.Name, "  ", localShapeIndex, bodyShapeIndex);
		switch ((EnemyCollisionArea)localShapeIndex)
		{
			case EnemyCollisionArea.BeAttack1:
				isBeAttack1 = true;
				break;
			case EnemyCollisionArea.BeAttack2:
				isBeAttack2 = true;
				break;
			case EnemyCollisionArea.AttactRange:
				isAttack = true;
				break;
			case EnemyCollisionArea.PursueRange:
				isPursue = true;
				break;
		}
		_heroBody = (Hero)body;
	}

	private void OnExitCollision(Rid bodyRid, Node2D body, long bodyShapeIndex, long localShapeIndex)
	{


		switch ((EnemyCollisionArea)localShapeIndex)
		{
			case EnemyCollisionArea.BeAttack1:
				isBeAttack1 = false;
				break;
			case EnemyCollisionArea.BeAttack2:
				isBeAttack2 = false;
				break;
			case EnemyCollisionArea.AttactRange:
				isAttack = false;
				break;
			case EnemyCollisionArea.PursueRange:
				isPursue = false;
				_heroBody = (Hero)body;
				break;
		}
		//GD.Print("离开了", body.Name, " ", isAttack, " ", isPursue, " ");

	}



	private int moveFlag = 1;
	private float moveSpeed = 1.2f;
	public void MoveRound()
	{
		if (this.Position.X <= 400)
		{
			moveFlag = 1;
		}
		else if (this.Position.X >= 800)
		{
			moveFlag = -1;
		}

		this.Position = new Vector2(this.Position.X + moveFlag * moveSpeed, this.Position.Y);

	}

	public void Stay()
	{
		//GD.Print("停下来停下来");
		_playBack.Travel("idle");
	}

	public void StopTree()
	{
		//暂停行为树
		var tree = FindChild("EnemyBasicTree");
		tree.Set("enabled", false);
	}

	public bool CheckBeAttack1()
	{
		return isBeAttack1;
	}

	public bool CheckBeAttack2()
	{
		return isBeAttack2;
	}

	public bool CheckIsPursue()
	{

		return isPursue && !isBeAttack1 && !isBeAttack2 && !isAttack;

	}

	public bool CheckIsAttack()
	{
		return !isBeAttack1 && !isBeAttack2 && isAttack;
	}

	public void MoveToHero()
	{

		CheckFace();
		_playBack.Travel("walk");
		GD.Print("英雄节点在哪", _heroBody.Position);
		Position = Position.MoveToward(_heroBody.Position, 0.3f);



	}

	public void Attack()
	{

		_playBack.Travel("attack");
	}

	public void BeAttack(int type)
	{
		//ClearAnimation();
		switch (type)
		{
			case 1:
				_playBack.Travel("beAttack1");
				break;
			case 2:
				_playBack.Travel("beAttack2");
				break;
		}
	}

	public void ClearAnimation()
	{
		_playerNode.Stop();
	}

	public void CheckFace()
	{
		//转向功能
		if (_heroBody != null)
		{
			if (Position.X > _heroBody.Position.X)
			{
				Scale = new Vector2(1, 1);
			}
			else
			{
				Scale = new Vector2(-1, 1);
			}
		}
	}
}


public enum EnemyCollisionArea
{
	BeAttack1 = 0,
	BeAttack2 = 1,
	AttactRange = 3,
	PursueRange = 2,
}


